<?php

namespace Registro\Common\Controllers;

use Phalcon\Mvc\Controller;
use Phalcon\Mvc\Dispatcher;

class ControllerBase extends Controller {

    protected function initialize()
    {
        $this->tag->prependTitle( 'MyGear | ' . $this->dispatcher->getModuleName() . ' | ' );
        $this->view->setViewsDir( '../apps/common/views/' );
    }

    /**
     * Execute before the router so we can determine if this is a provate controller, and must be authenticated, or a
     * public controller that is open to all.
     *
     * @param Dispatcher $dispatcher
     * @return boolean
     */
//    public function beforeExecuteRoute( Dispatcher $dispatcher )
//    {
//        $controllerName = $dispatcher->getControllerName();
//
//        // Only check permissions on private controllers
//        if( $this->acl->isPrivate( $controllerName ) )
//        {
//
//            // Get the current identity
//            $identity = $this->auth->getIdentity();
//
//            // If there is no identity available the user is redirected to index/index
//            if( !is_array( $identity ) )
//            {
//                $this->flash->notice( 'You don\'t have access to this module: private' );
//
//                $dispatcher->forward( array(
//                    'controller' => 'index',
//                    'action' => 'index'
//                ) );
//                return false;
//            }
//
//            // Check if the user have permission to the current option
//            $actionName = $dispatcher->getActionName();
//            if( !$this->acl->isAllowed( $identity['profile'], $controllerName, $actionName ) )
//            {
//                $this->flash->notice( 'You don\'t have access to this module: ' . $controllerName . ':' . $actionName );
//
//                if( $this->acl->isAllowed( $identity['profile'], $controllerName, 'index' ) )
//                {
//                    $dispatcher->forward( array(
//                        'controller' => $controllerName,
//                        'action' => 'index'
//                    ) );
//                }
//                else
//                {
//                    $dispatcher->forward( array(
//                        'controller' => 'auth',
//                        'action' => 'index'
//                    ) );
//                }
//
//                return false;
//            }
//        }
//    }
//    protected function forward( $uri )
//    {
//        $uriParts = explode( '/', $uri );
//        $params = array_slice( $uriParts, 2 );
//        return $this->dispatcher->forward(
//                        array(
//                            'controller' => $uriParts[0],
//                            'action' => $uriParts[1],
//                            'params' => $params
//                        )
//        );
//    }
}
